home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Text and Fonts / TextOnBaseline / TextOnBaseline.cs next >
Encoding:
Text File  |  2001-01-15  |  1.3 KB  |  44 lines

  1. //---------------------------------------------
  2. // TextOnBaseline.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class TextOnBaseline: PrintableForm
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new TextOnBaseline());
  13.      }
  14.      public TextOnBaseline()
  15.      {
  16.           Text = "Text on Baseline";
  17.      }
  18.      protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  19.      {
  20.           float yBaseline = cy / 2;
  21.           Pen   pen       = new Pen(clr);
  22.  
  23.                // Draw the baseline across the center of the client area.
  24.  
  25.           grfx.DrawLine(pen, 0, yBaseline, cx, yBaseline);
  26.  
  27.                // Create a 144-point font.
  28.  
  29.           Font font = new Font("Times New Roman", 144);
  30.  
  31.                // Get and calculate some metrics.
  32.  
  33.           float cyLineSpace = font.GetHeight(grfx);
  34.           int   iCellSpace  = font.FontFamily.GetLineSpacing(font.Style);
  35.           int   iCellAscent = font.FontFamily.GetCellAscent(font.Style);
  36.           float cyAscent    = cyLineSpace * iCellAscent / iCellSpace;
  37.  
  38.                // Display the text on the baseline.
  39.  
  40.           grfx.DrawString("Baseline", font, new SolidBrush(clr),
  41.                           0, yBaseline - cyAscent);
  42.      }
  43. }
  44.